package xyz.zyt123.hikvideo.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import xyz.zyt123.hikvideo.common.AjaxResult;

import static org.springframework.http.HttpStatus.OK;

/**
 * 全局异常拦截
 *
 * @author zhuyuntao
 */
@Slf4j
@ControllerAdvice("com.zyt123.hikvideo.controller")
public class RestExceptionHandler {

  @ResponseBody
  @ExceptionHandler(Throwable.class)
  @ResponseStatus(OK)
  public AjaxResult<Void> restExceptionHandler(HttpServletRequest req, Exception ex) {
    log.warn(String.format("请求%s失败", req.getRequestURI()), ex);
    return AjaxResult.fail(ex.getMessage());
  }
}
